package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //处理查询所有
    //@RequiredPermission(name = "部门列表", expression = "employee:list")
    @RequestMapping("list")
    public String list (Model model, QueryObject qo) {
        /*System.out.println("前登录用户是否有HR_MGR角色:" +
                SecurityUtils.getSubject().hasRole("HR_MGR"));

        System.out.println("当前登录用户是否有role:list权限:" +
                SecurityUtils.getSubject().isPermitted("role:list"));*/

        model.addAttribute("pageInfo", departmentService.query(qo));
        return "department/list";//找视图
    }

    //去新增
    //@RequiredPermission(name = "部门编辑", expression = "employee:input")
    @RequestMapping("input")
    public String input (Model model, Long id) {
        if (id != null) {
            model.addAttribute("department", departmentService.selectById(id));
        }
        return "/department/input";//找视图

    }

    //@RequiredPermission(name = "部门删除", expression = "employee:delete")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete (Long id) {
        try {
            //执行删除方法
            if (id != null) {
                departmentService.deleteById(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
    }

    //@RequiredPermission(name = "部门保存或更新", expression = "employee:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate (Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.updateById(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }


    }
}
